import java.util.*;

class Team {
    private String name;
    private int matchesPlayed;
    private int wins;
    private int draws;
    private int losses;
    private int goalsScored;
    private int goalsConceded;
    private int points;

    public Team(String name) {
        this.name = name;
        this.matchesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsScored = 0;
        this.goalsConceded = 0;
        this.points = 0;
    }

    public String getName() {
        return name;
    }

    public int getPoints() {
        return points;
    }

    public int getGoalDifference() {
        return goalsScored - goalsConceded;
    }

    public int getGoalsScored() {
        return goalsScored;
    }

    public void updateStats(int scored, int conceded) {
        matchesPlayed++;
        goalsScored += scored;
        goalsConceded += conceded;
        if (scored > conceded) {
            wins++;
            points += 3;
        } else if (scored == conceded) {
            draws++;
            points += 1;
        } else {
            losses++;
        }
    }

    @Override
    public String toString() {
        return String.format("%-15s %3d %3d %3d %3d %3d %3d %3d",
                name, matchesPlayed, wins, draws, losses, goalsScored, goalsConceded, points);
    }
}

public class FootballLeague {
    private List<Team> teams;

    public FootballLeague(List<String> teamNames) {
        teams = new ArrayList<>();
        for (String name : teamNames) {
            teams.add(new Team(name));
        }
    }

    public void recordMatch(String homeTeamName, String awayTeamName, int homeGoals, int awayGoals) {
        Team homeTeam = findTeam(homeTeamName);
        Team awayTeam = findTeam(awayTeamName);

        if (homeTeam == null || awayTeam == null) {
            System.out.println("球队名称错误，请检查输入！");
            return;
        }

        homeTeam.updateStats(homeGoals, awayGoals);
        awayTeam.updateStats(awayGoals, homeGoals);
    }

    private Team findTeam(String name) {
        for (Team team : teams) {
            if (team.getName().equalsIgnoreCase(name)) {
                return team;
            }
        }
        return null;
    }

    public void displayRankings() {
        teams.sort((t1, t2) -> {
            if (t2.getPoints() != t1.getPoints()) {
                return t2.getPoints() - t1.getPoints();
            } else if (t2.getGoalDifference() != t1.getGoalDifference()) {
                return t2.getGoalDifference() - t1.getGoalDifference();
            } else {
                return t2.getGoalsScored() - t1.getGoalsScored();
            }
        });

        System.out.println("排名表:");
        System.out.printf("%-15s %3s %3s %3s %3s %3s %3s %3s\n",
                "队名", "场次", "胜", "平", "负", "进球", "失球", "积分");
        for (int i = 0; i < teams.size(); i++) {
            System.out.printf("%-3d %s\n", i + 1, teams.get(i).toString());
        }
    }

    public static void main(String[] args) {
        // 初始化球队
        List<String> teamNames = Arrays.asList("广州队", "北京队", "上海队", "天津队", "重庆队");
        FootballLeague league = new FootballLeague(teamNames);

        Scanner scanner = new Scanner(System.in);

        // 输入比赛结果
        while (true) {
            System.out.println("请输入比赛结果(格式: 主队 客队 主队进球 客队进球), 输入'end'结束:");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("end")) {
                break;
            }

            String[] parts = input.split(" ");
            if (parts.length != 4) {
                System.out.println("输入格式错误！");
                continue;
            }

            String homeTeam = parts[0];
            String awayTeam = parts[1];
            int homeGoals, awayGoals;

            try {
                homeGoals = Integer.parseInt(parts[2]);
                awayGoals = Integer.parseInt(parts[3]);
            } catch (NumberFormatException e) {
                System.out.println("进球数必须为整数！");
                continue;
            }

            league.recordMatch(homeTeam, awayTeam, homeGoals, awayGoals);
        }

        // 输出排名表
        league.displayRankings();
    }
}
